#include "traytypeshow.h"
#include "ui_traytypeshow.h"
#include <QMouseEvent>
#include <QDebug>
#include <QTimerEvent>

TrayTypeShow::TrayTypeShow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TrayTypeShow)
{
    ui->setupUi(this);

    Tray_Type =1;
    Drop_Type=0;
    DropNum=0;
    DropArea=0;


    Rows_of_TrayType_1=10;
    Columns_of_TrayType_1=10;

    Rows_of_TrayType_2=6;
    Columns_of_TrayType_2=6;

    Rows_of_TrayType_3=5;
    Columns_of_TrayType_3=5;


    //启动定时器
    id1=startTimer(50);//每50ms调用一次
    id2=startTimer(2000);

}

TrayTypeShow::~TrayTypeShow()
{
    delete ui;
}

void TrayTypeShow::paintEvent(QPaintEvent *)
{
    Drawtray();
}

void TrayTypeShow::SetTrayType(int type)
{
    Tray_Type = type;
    update();
}

unsigned char TrayTypeShow::GetTrayType(void)
{

    return Tray_Type;
}

void TrayTypeShow::SetDripNum(int num)
{
    DropNum = num;
    update();
}
void TrayTypeShow::SetDropType(int Type)
{
    Drop_Type = Type;
    update();
}

void TrayTypeShow::SetDropArea(int Area)
{
    Drop_Type = Area;
    update();
}

void TrayTypeShow::mousePressEvent(QMouseEvent *event)
{
    int x_count=0,y_count=0;
    qDebug()<<event->x()<<event->y();

    switch(Tray_Type){
    case 1:{//30mm盘型
        for (int x=1;x<11;x++)
        {
            if((event->x()<x*60))
            {
                x_count=x;
                break;
            }

        }
        for (int y=1;y<11;y++)
        {
            if((event->y()<y*60))
            {
                y_count=y;
                break;
            }
        }
        //修改
        x_cnt = x_count;
        y_cnt = y_count;
        //修改一
        // DropNum=x_count+((y_count-1)*10);

        if(x_count <= 5){
            if(y_count <= 5){
                DropNum = x_count+((y_count-1)*5);
                DropArea = 1;
            }else{
                DropNum = x_count+((y_count-1 - 5)*5);
                DropArea = 3;
            }

        }else{
            if(y_count <= 5){
                DropNum = (x_count - 5)+((y_count-1)*5);
                DropArea = 2;
            }else{
                DropNum = (x_count - 5)+((y_count-1 - 5)*5);
                DropArea = 4;
            }

        }
        break;
    }
    case 2:{//60mm盘型
        for (int x=1;x<7;x++)
        {
            if((event->x()<x*100))
            {
                x_count=x;
                break;
            }

        }
        for (int y=1;y<7;y++)
        {
            if((event->y()<y*100))
            {
                y_count=y;
                break;
            }
        }
        //修改
        x_cnt = x_count;
        y_cnt = y_count;
        //修改一
        // DropNum=x_count+((y_count-1)*10);

        if(x_count <= 3){
            if(y_count <= 3){
                DropNum = x_count+((y_count-1)*3);
                DropArea = 1;
            }else{
                DropNum = x_count+((y_count-1 - 3)*3);
                DropArea = 3;
            }

        }else{
            if(y_count <= 3){
                DropNum = (x_count - 3)+((y_count-1)*3);
                DropArea = 2;
            }else{
                DropNum = (x_count - 3)+((y_count-1 - 3)*3);
                DropArea = 4;
            }

        }
        break;
    }
    case 3:{//方型盘
        for (int x=1;x<7;x++)
        {
            if((event->x()<x*120))
            {
                x_count=x;
                break;
            }

        }
        for (int y=1;y<7;y++)
        {
            if((event->y()<y*120))
            {
                y_count=y;
                break;
            }
        }
        //修改
        x_cnt = x_count;
        y_cnt = y_count;
        //修改一
        // DropNum=x_count+((y_count-1)*10);

        if(y_count <= 2){//区域1
            DropNum = x_count+((y_count-1)*5);
            DropArea = 1;
        }else{//区域2
            DropNum = x_count+((y_count-3)*5);
            DropArea = 2;
        }

        break;
    }
    }


    qDebug()<<"x="<<x_count<<"y="<<y_count<<"dropnum="<<DropNum<<"DropArea="<<DropArea;
    update();
}
//定时器事件
void TrayTypeShow::timerEvent(QTimerEvent *e)
{
    if(e->timerId()==id1){


    }

    if(e->timerId()==id2){
//    TrayTypeShow::Drawtray();

    }
}



void TrayTypeShow::Drawtray()
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing,  true);
    painter.setPen(QPen(QColor(140,140,140),4,Qt::SolidLine));
    // painter.setBrush(QColor(140,140,140));

    int DripNumCount = 0;

    //修改
    int DrawFlag = 0;

    if(Tray_Type == 1)
    {
        int x_offset = 10, y_offset = 10;
        // 计算外框线的宽度和高度
        int width = Columns_of_TrayType_1 * 60 + 10;
        int height = Rows_of_TrayType_1 * 60 + 10;
        // 绘制外框线
        // painter.setBrush(QColor(255,140,140));
        painter.setPen(QPen(QColor(255,215,0),4,Qt::SolidLine));
        painter.drawRect(1, 1, width-1, height-1);
        //绘制内框线
        QLineF pointL[2];
        pointL[0].setLine(width/2, 0, width/2, height);
        pointL[1].setLine(0, height/2, width, height/2);
        painter.drawLines(pointL, 2);
        painter.setPen(QPen(QColor(140,140,140),4,Qt::SolidLine));
        painter.setBrush(QColor(140,140,140));
        for (int j = 0;j < Rows_of_TrayType_1;j++) {
            for (int i = 0;i < Columns_of_TrayType_1;i++) {
                if(i < 5 && j < 5){
                    x_offset = 5;
                    y_offset = 5;
                }else if(i >= 5 && j < 5){
                    x_offset = 15;
                    y_offset = 5;
                }else if(i < 5 && j >= 5){
                    x_offset = 5;
                    y_offset = 15;
                }else if(i >= 5 && j >= 5){
                    x_offset = 15;
                    y_offset = 15;
                }
                painter.drawEllipse(x_offset + 60 * i ,y_offset + 60 * j ,50 ,50);
                if(DripNumCount<DropNum)//绘制颜色块 要根据
                {
                    if(x_cnt <= 5){
                        if(y_cnt <= 5){//区域一
                            if(j < 5 && i < 5){
                                DrawFlag = 1;
                            }
                        }else{//区域三
                            if(j >= 5 && i < 5){
                                DrawFlag = 1;
                            }

                        }
                    }else{
                        if(y_cnt <= 5){//区域二
                            if(j < 5 && i >= 5){
                                DrawFlag = 1;
                            }
                        }else{//区域四
                            if(j >= 5 && i >= 5){
                                DrawFlag = 1;
                            }

                        }
                    }
                    if(DrawFlag){
                        // painter.setBrush(QColor(255,0,0));
                        painter.setBrush(QColor(255,0,0));
                        painter.drawEllipse(x_offset+3 + 60 * i ,y_offset+3 + 60 * j ,44 ,44);
                        painter.save();
                        painter.setPen(Qt::black);
                        DrawDrip(8+x_offset + 60 * i ,15+y_offset + 60 * j );
                        painter.restore();
                        painter.setBrush(QColor(140,140,140));
                        DripNumCount++;
                        DrawFlag = 0;
                    }
                }
            }

        }

    }

    //60mm板型
    else if(Tray_Type == 2)
    {
        int x_offset = 6, y_offset = 6;
        // 计算外框线的宽度和高度
        int width = Columns_of_TrayType_2 * 100 + 10;
        int height = Rows_of_TrayType_2 * 100 + 10;
        // 绘制外框线
        painter.setPen(QPen(QColor(255,215,0),4,Qt::SolidLine));
        painter.drawRect(1, 1, width-1, height-1);
        //绘制内框线
        QLineF pointL[2];
        pointL[0].setLine(width/2, 0, width/2, height);
        pointL[1].setLine(0, height/2, width, height/2);
        painter.drawLines(pointL, 2);
        painter.setPen(QPen(QColor(140,140,140),4,Qt::SolidLine));
        painter.setBrush(QColor(140,140,140));
        for (int j = 0;j < Rows_of_TrayType_2;j++) {
            for (int i = 0;i < Columns_of_TrayType_2;i++) {
                if(i < 3 && j < 3){
                    x_offset = 5;
                    y_offset = 5;
                }else if(i >= 3 && j < 3){
                    x_offset = 15;
                    y_offset = 5;
                }else if(i < 3 && j >= 3){
                    x_offset = 5;
                    y_offset = 15;
                }else if(i >= 3 && j >= 3){
                    x_offset = 15;
                    y_offset = 15;
                }
                painter.drawEllipse(x_offset + 100 * i ,y_offset + 100 * j ,90 ,90);
                if(DripNumCount<DropNum)//绘制颜色块 要根据
                {
                    if(x_cnt <= 3){
                        if(y_cnt <= 3){//区域一
                            if(j < 3 && i < 3){
                                DrawFlag = 1;
                            }
                        }else{//区域三
                            if(j >= 3 && i < 3){
                                DrawFlag = 1;
                            }

                        }
                    }else{
                        if(y_cnt <= 3){//区域二
                            if(j < 3 && i >= 3){
                                DrawFlag = 1;
                            }
                        }else{//区域四
                            if(j >= 3 && i >= 3){
                                DrawFlag = 1;
                            }

                        }
                    }
                    if(DrawFlag){
                        painter.setBrush(QColor(255,0,0));
                        painter.drawEllipse(x_offset+5 + 100 * i ,y_offset+5 + 100 * j ,80 ,80);
                        painter.save();
                        painter.setPen(Qt::black);
                        DrawDrip(8+x_offset+20 + 100 * i ,18+y_offset+20 + 100 * j );
                        painter.restore();
                        painter.setBrush(QColor(140,140,140));
                        DripNumCount++;
                        DrawFlag = 0;
                    }
                }
            }
        }

    }
    //方板型
    else if(Tray_Type == 3)
    {
        int x_offset = 5, y_offset = 5;
        // 计算外框线的宽度和高度
        int width = Columns_of_TrayType_3 * 120 + 10;
        int height = Rows_of_TrayType_3 * 120 + 10;
        // 绘制外框线
        painter.setPen(QPen(QColor(255,215,0),4,Qt::SolidLine));
        painter.drawRect(1, 1, width-1, height-1);
        //绘制内框线
        QLineF pointL[2];

        pointL[0].setLine(5, 5+height*0.4, 5+width, 5+height*0.4);
        painter.drawLines(pointL, 2);
        painter.setPen(QPen(QColor(140,140,140),4,Qt::SolidLine));
        painter.setBrush(QColor(140,140,140));

        for (int j = 0;j < Rows_of_TrayType_3;j++) {
            for (int i = 0;i < Columns_of_TrayType_3;i++) {

                x_offset = 10;
                y_offset = j >= 2 ? 15:10;
                painter.drawRect(x_offset + 120 * i ,y_offset + 120 * j ,110 ,110);
                if(DripNumCount<DropNum)//绘制颜色块 要根据
                {

                    if(j<=1){
                        DrawFlag = 1;
                    }else{
                        DrawFlag = 2;
                    }
                    if(DrawFlag == DropArea){
                        painter.setBrush(QColor(255,0,0));
                        painter.drawRect(x_offset+3 + 120 * i ,y_offset+3 + 120 * j ,100 ,100);
                        painter.save();
                        painter.setPen(Qt::black);
                        DrawDrip(5+40 + 120 * i ,15+45 + 120 * j );
                        painter.restore();
                        painter.setBrush(QColor(140,140,140));
                        DripNumCount++;
                        DrawFlag = 0;
                    }
                }
            }
        }
        qDebug()<<"DripNumCount="<<DripNumCount;

    }
}
void TrayTypeShow::DrawDrip(int x, int y)
{
    QPainter painter2(this);
    painter2.setPen(QPen(QColor(0,255,0),4,Qt::SolidLine));
    painter2.setBrush(QColor(0,255,0));
    switch(Drop_Type)
    {
        case 0:
            break;
        case 1:
            painter2.drawText(x,y,40,40,Qt::AlignHCenter|Qt::AlignTop,"1");
            break;
        case 2:
            painter2.drawText(x,y,40,40,Qt::AlignHCenter|Qt::AlignTop,"2");
            break;
        case 3:
            painter2.drawText(x,y,40,40,Qt::AlignHCenter|Qt::AlignTop,"3");
            break;
        case 4:
            painter2.drawText(x,y,40,40,Qt::AlignHCenter|Qt::AlignTop,"4");
            break;
        case 6:
            painter2.drawText(x,y,40,40,Qt::AlignHCenter|Qt::AlignTop,"6");
            break;


    }
}

